﻿/*  Get project list by user    */
function GetProjectListByUserId(userId) {
    $.ajax({
        url: "/ManageActualResource/GetProjectPartialByUser",
        type: 'POST',
        data: { userId: userId },
        success: function (response) {
            $("#ajaxDropDownListProject").replaceWith(response);
            ClearGridData();
        },
        error: function (xhr) {
            alert("Error!");
        }
    });
}

/*  Get project detailed information    */
function GetProjectDetail(pr_ref) {
    $.ajax({
        url: "/ManageActualResource/GetProjectDetail",
        type: 'POST',
        data: { projectId: pr_ref },
        success: function (response) {

            // Split response to array of values
            var array = response.split("#");

            // Replace old values with new ones
            $("#inputEngineer").val(array[0]);
            $("#inputLeadEngineer").val(array[1]);
            $("#inputToStart").val(array[2]);
            $("#inputToFinish").val(array[3]);
            $("#inputStatus").val(array[4]);
            $("#inputCompleted").val(array[5]);


        },
        error: function (xhr) {
            alert("Error!");
        }
    });
}

function GetProjectData() {
    if ($("select#ddlProject").val() != "-1" && $("select#ddlProject").val() != "" && $("select#ddlProject").val() != null) {
        // Get project referenced value
        var pr_ref = $("select#ddlProject").val();

        // Get project detailed info and categories, other engineers
        GetProjectDetail(pr_ref);

        // Ajax function
        $.ajax({
            url: "/ManageActualResource/GetTask",
            type: 'POST',
            data: {projectId: pr_ref},
            success: function (response) {$("#ajaxGridTask").replaceWith(response)},
            error: function (xhr) {alert("Error!");}
        });
    }
    else {
        // Clear all grid's data
        ClearGridData();
    }
}


/*  Clear all grid data    */
function ClearGridData() {
    $("#ajaxGridCategory").replaceWith("<div id='ajaxGridCategory'></div>");
    $("#ajaxGridTask").replaceWith("<div id='ajaxGridTask'></div>");
    $("#ajaxListOtherEngineers").replaceWith("<div id='ajaxListOtherEngineers'></div>");
}


function GetLoadingDialog() {
    var dialog = $('#LoadingDialog');
    dialog.html('<div><img src="../../Images/ajax-loader.gif" /></div>');
    dialog.dialog({
        closeOnEscape: false,
        dialogClass: "no-title",
        bgiframe: true,
        resizable: false,
        modal: true,
        width: 250,
        height: 50
    });

}
function CloseLoadingDialog() {
    var dialog = $('#LoadingDialog');
    dialog.dialog("close");
}

function GetEditActualResourceDialog(_genActualResourceUsageId) {
    GetLoadingDialog();
    var dialog = $('#EditActualResourceDialog');
    $.get("/ManageActualResource/GetEditActualResourceDialog",
        { genActualResourceUsageId: _genActualResourceUsageId },
        function (responseText) {
            dialog.html(responseText);
            dialog.dialog({
                closeOnEscape: false,
                dialogClass: "no-close",
                bgiframe: true,
                resizable: false,
                modal: true,
                width: 450,
                buttons: {
                    // button OK
                    "OK": function () {
                        var startDate = $('#inputStartDate').val() + " " + $('#inputStartTime').val();
                        var endDate = $('#inputEndDate').val() + " " + $('#inputEndTime').val();

                        // Show Enter date dialog
                        UpdateActualResourceUsage(_genActualResourceUsageId, startDate, endDate);

                        // Close this dialog
                        $(this).dialog("close");
                    },
                    "Cancel": function () {
                        // Close this dialog
                        $(this).dialog("close");
                    }
                }
            });
        });
    CloseLoadingDialog();
}

/*  Get project list by user    */
function UpdateActualResourceUsage(_genActualResourceUsageId, startDate, endDate) {
    if (startDate != null && endDate != null && _genActualResourceUsageId != null) {
        $.ajax({
            url: "/ManageActualResource/UpdateActualResourceUsage",
            type: 'POST',
            data: {
                genActualResourceUsageId: _genActualResourceUsageId,
                startDate: startDate,
                endDate: endDate
            },
            success: function (response) {
                if (response != null && response != "") {
                    if (response == "-1") {
                        var message = "DateTime string was not in correct format !";
                        alert(message);
                    }
                    else {
                        var message = "Successfully edit actual resource usage row !";
                        alert(message);
                        var currentTaskId = $('#txtTaskId').val();
                        LoadActualResourceUsage(currentTaskId);
                    }
                }
            },
            error: function (xhr) {
                alert("Error!");
            }
        });
    }
}

function LoadActualResourceUsage(currentTaskId) {


    // Ajax function load actual resource usages
    $.ajax({
        url: "/ManageActualResource/GetActualResourceOfTask",
        type: 'POST',
        data: {
            TaskId: currentTaskId
        },
        success: function (response) {
            $("#ajaxGridActualResource").replaceWith(response);
        },
        error: function (xhr) {
            alert("Error loading actual resources!");
        }
    });
}